﻿using System.ComponentModel.DataAnnotations;

namespace Homework16_2_
{
    /// <summary>
    /// produce 10000 samples, include id, name, age, birthdate, height, gender, D:\samples.txt
    /// </summary>

    class Person
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
        public DateTime BirthDate { get; set; }
        public double Height { get; set; }
        public string Gender { get; set; }
    }

    internal class Program
    {
        //样本数量为10000
        private const int CountOfSample = 10000;

        //随机数生成器
        private static Random random = new Random();

        //存入文件路径
        private const string FilePath = @"D:\samples(2).txt";

        static void Main(string[] args)
        {
            //错误防范
            try
            {
                var persons = Enumerable.Range(1, CountOfSample).Select(i =>
                {
                    //姓名由3个随机字母组成且首字母大写
                    var name = RandomName(3);

                    //随机生成年龄，范围为20-60
                    var age = random.Next(20, 61);

                    //由随机生成的年龄计算出生年份
                    var birthYear = DateTime.Now.Year - age;

                    //随机生成月份与日期，和上一步算出的年份，一起组成具体出生日期
                    var birthDate = new DateTime(birthYear, random.Next(1, 13), random.Next(1, 29));

                    //随机生成身高，范围为140-200cm
                    var height = random.Next(140, 201);

                    //随机生成性别，0为male，1为female
                    var gender = random.Next(0, 2) == 0 ? "Male" : "Female";

                    //创建一个新的person对象并返回
                    return new Person
                    {
                        Id = i,
                        Name = name,
                        Age = age,
                        BirthDate = birthDate,
                        Height = height,
                        Gender = gender,
                    };

                }).ToList();

                //文件路径
                string filepath = FilePath;

                //遍历所有行
                File.WriteAllLines(filepath, persons.Select(p =>
                     $"{p.Id},{p.Name},{p.Age},{p.BirthDate:yyyy-MM-dd},{p.Height},{p.Gender}"));

                //写入成功提示
                Console.WriteLine("已成功写入" + filepath);
            }

            //try中若出错，显示出错信息
            catch (Exception ex) 
            {
                Console.WriteLine($"发生错误：{ex.Message}");
            }
        }

        private static string RandomName(int length)
        {
            //从26个字母中随机取3个字母组成姓名
            var chars = "abcdefghijklmnopqrstuvwxyz";

            //随机生成首字母且姓名首字母大写
            char FirstLetter = char.ToUpper(chars[random.Next(chars.Length)]);

            //随机生成后两个字母
            string Letters = new string(Enumerable.Repeat(chars, length - 1).Select(s => 
                             s[random.Next(s.Length)]).ToArray());
            //得到姓名
            return $"{FirstLetter}{Letters}";
        }
    }
}
